package org.gap.wizards.spells;

import org.gap.wizards.creature.Creature;
import org.gap.wizards.creature.SelectCreature;
import org.gap.wizards.effects.Effect;
import org.gap.wizards.pc.skills.Challenge;
import org.gap.wizards.views.ViewPlatform;
import org.gap.wizards.world.Location;

public class TargetableSpell implements Spell {

	private final SelectCreature targetSelector;
	private final Effect effect;
	private final Description description;
	private Creature creature;

	public TargetableSpell(Description description, SelectCreature target, Effect effect) {
		this.description = description;
		this.targetSelector = target;
		this.effect = effect;
	}
	
	@Override
	public String cast(Location location, Challenge challenge) {
		creature = targetSelector.select(location);
		if (creature != null) {
			switch(challenge.perform()) {
				case SUCCESS:
					effect.apply(creature);
					return description.describeSuccessfulEffect();
				case FAILURE:
				default:
					return description.describeFailEffect();
			}
		}
		return "No creature was selected";
	}

	@Override
	public void costOfSpell(Creature creature) {
		description.applyCost(creature);
	}
	
	@Override
	public String name() {
		return description.getName();
	}
	
	@Override
	public void showAffect(Location location, ViewPlatform platform) {
		description.showEffect(location, creature, platform);
	}
	
	@Override
	public String toString() {
		return description.toString();
	}
}
